package cn.lzg.server;

import org.springframework.stereotype.Component;

import cn.lzg.common.core.codec.RequestDecoder;
import cn.lzg.common.core.codec.ResponseEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

@Component
public class Server {
	/**
	 * 启动服务器
	 */
	public void start() {
		//服务启动类
		ServerBootstrap bootstrap = new ServerBootstrap();
		
		//boss&worker
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		
		try {
			//设置参数
			//绑定端口
			bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
				.option(ChannelOption.SO_BACKLOG, 2048)
				.childHandler(new ChannelInitializer<SocketChannel>() {

					@Override
					protected void initChannel(SocketChannel ch) throws Exception {
						ch.pipeline().addLast(new RequestDecoder());
						ch.pipeline().addLast(new ResponseEncoder());
						ch.pipeline().addLast(new ServerHandler());
					}
				}).bind(8899).sync();
			System.out.println("start~");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
